<template>
    <div class="content">
        <div class="room-list">
            <div class="room-item" v-for="item in hallList" :key="item.id">
                <book-item toShop :item="item" />
            </div>
        </div>
        <u-loadmore
            loadmoreText="找自习 来上岸"
            fontSize="24rpx"
            color="#D5D5D5"
            lineColor="#D5D5D5"
            marginTop="20"
            line
        />
    </div>
</template>

<script>
import BookItem from '@/components/BookItem';
export default {
    name: 'room',
    components: { BookItem },
    data() {
        return {
            params: {
                pageNo: 1,
                pageSize: 20,
            },
            hallList: [],
            total: 0,
            empty: false,
            finish: false,
        };
    },
    onShow() {
        this.refreshList();
    },
    onPullDownRefresh() {
        this.refreshList();
    },
    onReachBottom() {
        this.refreshList(true);
    },
    methods: {
        getHallList(load) {
            if (this.hallList < this.total || (!this.empty && !this.finish)) {
                this.$Api.study.hall
                    .page({
                        ...this.params,
                        longitude: getApp().globalData.longitude,
                        latitude: getApp().globalData.latitude,
                    })
                    .then(res => {
                        if (!load) {
                            this.hallList = res.list;
                        } else {
                            this.hallList = this.hallList.concat(res.list);
                        }
                        this.params.pageNo++;
                        this.total = res.total;
                        if (res.total === 0) {
                            this.empty = true;
                        }
                        if (this.hallList.length === res.total) {
                            this.finish = true;
                        }
                        uni.stopPullDownRefresh();
                    });
            }
        },
        refreshList(load) {
            if (!load) {
                this.hallList = [];
                this.total = 0;
                this.empty = false;
                this.finish = false;
                this.params.pageNo = 1;
            }
            this.getHallList();
        },
    },
};
</script>

<style lang="scss" scoped>
.content {
    padding: 20rpx 30rpx;
    .room-list {
        .room-item {
            margin-bottom: 20rpx;
            background: #fff;
            border-radius: 20rpx;
        }
    }
}
</style>
